use "pseudo_data", clear

********************************************************************************************
*Data: this is lender position level data (per loan many rows) that will be aggregated at the loan level. The data provided is pseudo-data. All observations in the pseudo *data are randomized data points of the de-identified original data. All bank, loan, deal and borrower identifiers have been replaced to preserve confidentiality. Within *observation structures and correlations are not preserved. To preserve confidentiality, the assignment of loans to banks (in the loan-level dataset) has also been randomized *to ensure that the original structure of the data in terms of loans per bank is not preserved. The original ids have been replaced with randomly assigned groups. The loan-*level dataset includes only the sub-set of loans.
*********************************************************************************************

/// VARIABLE DEFINITIONS 

* first_year: indicator variable taking one for the first year of the loan; used for loan level regressions because loan spread is available only at the year of origination 
* amt: loan amout of a lender investment
* amt_tot:  total loan amount 
* amt_outstaning: amount outstaning 
* capital: the average syndicate level capital used in Table 5 
* capital_alt: average syndicate level capital used in Table B.4
* matyr is the yearly difference between matyrity year and current year 
* wbkroalead Yearly lagged lead bank ROA based on Y-9C/Call Reports 
* wbkncolead Yearly lagged net-chargeoffs based on Y-9C/Call Reports  
* wassets_deal_clead Yearly lagged assets  based on Y-9C/Call Reports 
* wtier1_lead Yearly lagged tier 1 capital of the lead  based on Y-9C/Call Reports
* variables wbkroaleadP, wbkncoleadP, wassets_deal_cleadP, wassets_deal_cleadP, wtier1_leadP are aggreagted at the package-year level. 

* loan_id_mask if the loan ID 
* borrower_id_mask if the company ID 
* lender id is the bank idssd 
* lead_id_mask  is the lead bank id (it it masked and randomized; not related to lender id)
* deal_id_mask is deal/package id
* note that regressions in table 4 cannot be estimated because of insufficial observations 

* Variable IDs 

egen ctyr=group(loan_id_mask year)
sort ctyr
g loan=1 if ctyr!=ctyr[_n-1]   

egen packtime=group(deal_id_mask year)
sort packtime
g pack_count=1 if packtime!=packtime[_n-1]  

egen bankyr=group(lender_id_mask year)
sort bankyr
g bkyr_1=1 if bankyr!=bankyr[_n-1] 

egen boryr=group(borrower_id_mask year)
sort boryr
g boryr_1=1 if boryr!=boryr[_n-1] 

egen boryear=group(borrower_id_mask year) //borrower-year 
egen byr=group(lender_id_mask year) //bank-year 

* 3 year period 
g yr3=. 
replace yr3=1 if year>=1991 & year<=1993
replace yr3=2 if year>=1994 & year<=1996
replace yr3=3 if year>=1997 & year<=1999
replace yr3=4 if year>=2000 & year<=2002
replace yr3=5 if year>=2003 & year<=2005
replace yr3=6 if year>=2006 & year<=2008
replace yr3=7 if year>=2009 & year<=2011
replace yr3=8 if year>=2012 & year<=2014
replace yr3=9 if year>=2015 

egen boryr3=group(borrower_id_mask yr3) //used for regressions (firm*time fixed effects) 

* NBER Recessions
  
g nber=(year==1990 | year==1991 | year==2001 | year==2008 | year==2009 | year==1992 | year==2002 |year==2007 | year==2000 | year==2010 )
 
****************************************************** 
// Definition of lender categories and lender shares 
******************************************************

g bank=(cat_top==1 | cat_top==2 | cat_top==3 | cat_top==4)  
egen amt_new=sum(amt) if bank==1 , by(loan_id_mask year)    
g sh_new=amt/amt_new if bank==1	
 
g shadow_banks=(cat_top==7 | cat_top==8 | cat_top==10 | cat_top==11 |  cat_top==12 |  cat_top==13 |  cat_top==14 | cat_top==16) 
g funds=amt/amt_tot if shadow_banks==1  
replace funds=0 if funds==. 
egen shadow_share=sum(funds), by(loan_id_mask year)  //Dependent variable for table 3 


********************************************************
// loan types
********************************************************

g cr_line=(LoanType=="364-Day Facility" | LoanType=="Revolver/Line < 1 Yr." | LoanType=="Revolver/Line >= 1 Yr." |  LoanType=="Revolver/Term loan" | LoanType=="Performance Standby Letter of Credit" | LoanType=="Standby Letter of Credit" |LoanType=="Trade Letter of Credit" |  LoanType=="Limited Line") 

g term_loan=(LoanType=="Acquisition Facility" |LoanType=="Bridge Loan" | LoanType=="CAPEX Facility" |LoanType=="Construction Facility" | LoanType=="Delay Draw Term Loan" | LoanType=="Demand Loan" | LoanType=="Multi-Option Facility" | LoanType=="Other Loan" | LoanType=="Lease" | LoanType=="Synthetic Lease" | LoanType=="Term Loan" | LoanType=="Term Loan A" | LoanType=="Term Loan B" | LoanType=="Term Loan C" | LoanType=="Term Loan D" | LoanType=="Term Loan E" |LoanType=="Term Loan F" | LoanType=="Term Loan G" | LoanType=="Term Loan H" |  LoanType=="Term Loan I" |  LoanType=="Term Loan J") 

g term_loanA=(LoanType=="Term Loan A")
g term_loanAplus=(LoanType=="Term Loan A" | LoanType=="Term Loan" | LoanType=="Delay Draw Term Loan")
g term_loanB=(LoanType=="Term Loan B")
g term_loanBplus=(LoanType=="Term Loan C" | LoanType=="Term Loan D" | LoanType=="Term Loan E" | LoanType=="Term Loan F" |  LoanType=="Term Loan G" | LoanType=="Term Loan H" |  LoanType=="Term Loan I" |  LoanType=="Term Loan J")

g term_loanBplus1=(term_loanB==1 | term_loanBplus==1)
replace term_loanBplus1=0 if term_loanAplus==1

***************************************
// Package/Deal types
***************************************
egen maxcl=max(cr_line), by(deal_id_mask year) //one indicated that there is at least one credit line per package-year
egen maxtlA=max(term_loanA), by(deal_id_mask year)  //one indicated that there is at least one term-A loans package-year
egen maxtlB=max(term_loanBplus1), by(deal_id_mask year)
egen maxtl=max(term_loan), by(deal_id_mask year)
 
g packA=(maxcl==1 & maxtlA==1 & maxtlB==0)
g packB=(maxcl==1 & maxtlB==1 & maxtlA==0)
g packAB=(maxcl==1 & maxtlB==1 & maxtlA==1 )  //pakcage A & B;  used in the regressions as TL A& B package (ex: table 3, column 4)
g packAB_noCL=(maxcl==0 & maxtlB==1 & maxtlA==1 ) 
 
g packA_B=.  //package A or B; used in the regressions as TL B package (ex: table 3, columns 1-3)
replace packA_B=0 if packA==1   //only deal A 
replace packA_B=1 if packB==1  //only deal B 

*************************************************************************
**************************************************************************
*** Define B vs A firm if >50% of funding (ex used in Table 7, TL B firm)
**************************************************************************

egen packAmax=max(packA), by(borrower_id_mask) 
egen packBmax=max(packB), by(borrower_id_mask) 
egen packABmax=max(packAB), by(borrower_id_mask)

g firm_pack=(packAmax==1 | packBmax==1| packABmax==1)   //sample of firms with these packages 
egen fpack=max(firm_pack), by(borrower_id_mask) //this is the sample of firms 

egen amt_f=sum(amt) if fpack==1, by(borrower_id_mask year)
egen  maxtlAfa=sum(amt) if fpack==1 &  term_loanA==1, by(borrower_id_mask year)
egen maxtlAf=max(maxtlAfa) if fpack==1, by(borrower_id_mask year)
egen  maxtlBfa=sum(amt) if fpack==1 &  term_loanBplus1==1, by(borrower_id_mask year)
egen maxtlBf=max(maxtlBfa) if fpack==1, by(borrower_id_mask year)
 
g ffundA=maxtlAf/amt_f
g ffundB=maxtlBf/amt_f

g ffundAd=(ffundA>0.5)
replace ffundAd=. if ffundA==.

g ffundBd=(ffundB>0.5)
replace ffundBd=. if ffundB==.

g tlb_firm=1 if ffundBd==1 
replace tlb_firm=0 if ffundAd==1

**************************************************************************
*** CONTROL VARIABLES 
**************************************************************************
**************************************************************************
*** Controls as the loan level  
**************************************************************************

g purp=0
replace purp=1 if PrimaryPurpose=="Acquis. line" |  PrimaryPurpose=="Merger" | PrimaryPurpose=="Takeover"
replace purp=2 if PrimaryPurpose=="Stock buyback" |  PrimaryPurpose=="Dividend Recap" | PrimaryPurpose=="Dividend or Distribution to Sharehold.."
replace purp=3 if PrimaryPurpose=="Corp. purposes" |  PrimaryPurpose=="Work. cap." 

g lnlsize=ln(amt_tot) //loan size 
g pass=(disp_p>50) // passing grade 
g wlnassetslead=log(wassets_deal_clead/1000000)   //in billion 

**************************************************************************
*** Controls as the package level  
**************************************************************************

egen amt_pack1=sum(amt_tot) if loan==1, by(deal_id_mask year)
egen amt_pack=max(amt_pack1), by(deal_id_mask year)
g lnsize=ln(amt_pack)

egen purpp=median(purp), by(deal_id_mask year)
egen pass_mean_pur=mean(disp_p), by(deal_id_mask year)
g pass_deal=(pass_mean_pur>50)

g lnassets_deal_lead=log(wassets_deal_clead/1000000)   //in billion  
 

***************************************************************************
*** DEPENDENT VARIABLES 
**************************************************************************
**************************************************************************
*** Loan spead, table 4 (at the loan issuance level only, no time varaition
**************************************************************************

egen aisd=max(AllInDrawn), by(loan_id_mask) //spread this variable for by loan 

**************************************************************************
*** CONCENTRATIONS  (Table 6; Table B.5)
**************************************************************************

/// HHI  (Table 6) 
	
g sh_new2=sh_new*sh_new
egen hhi=sum(sh_new2), by(loan_id_mask year) 	

/// Number of banks (Table B.5)					
g one=1
egen banks_numb=count(one) if bank==1, by(loan_id_mask year)
egen banks_number=max(banks_numb), by(loan_id_mask year)
g ln_numb_bks=ln(banks_number) 


**************************************************************************
*** Maturity difference at the package level (table 7)
**************************************************************************
egen matcl=mean(matyr)  if  cr_line==1 & loan==1, by(deal_id_mask year)
egen mat_cl=max(matcl), by(deal_id_mask year)

egen mattl=mean(matyr)  if term_loan==1 & loan==1, by(deal_id_mask year)
egen mat_tl=max(mattl), by(deal_id_mask year)

g mat_diff=mat_cl-mat_tl //table 7, column 1-3

* table 7, column 4, package A & B 
egen mtclab=mean(matyr)  if packAB==1 & cr_line==1, by(deal_id_mask year)
egen mt_clab=max(mtclab) if  packAB==1, by(deal_id_mask year)
g mat_diffab=mt_clab-matyr if packAB==1 & term_loan==1 // there are two observations per package-year: CL/term loan A and CL/terl loan B 

* table 7, column 5, at the firm level 

egen matclf=mean(matyr)  if cr_line==1 & loan==1 & fpack==1, by(borrower_id_mask year)
egen mat_clf=max(matclf), by(borrower_id_mask year)

egen mattlf=mean(matyr)  if term_loan==1 & loan==1 & fpack==1, by(borrower_id_mask year)
egen mat_tlf=max(mattlf), by(borrower_id_mask year)

g mat_diff_f=mat_clf-mat_tlf 

**************************************************************************
*** Amount difference at the package level (table 8)
**************************************************************************
egen amtcl=sum(amt)  if cr_line==1, by(deal_id_mask year)
egen amt_cl=max(amtcl), by(deal_id_mask year)

egen amttl=sum(amt)  if term_loan==1, by(deal_id_mask year)
egen amt_tl=max(amttl), by(deal_id_mask year)

g amt_diff=amt_cl/amt_tl  //table 8, column 1-3


* table 8, column 4, package A & B 
egen amtclab=sum(amt) if packAB==1 & cr_line==1, by(deal_id_mask year)
egen amt_clab=max(amtclab) if  packAB==1, by(deal_id_mask year)

egen amt_tl3=sum(amt) if packAB==1 & term_loan==1, by(loan_id_mask year)
egen amt_tl1=max(amt_tl3) if packAB==1, by(loan_id_mask year)

g amt_diffab=amt_clab/amt_tl1 if packAB==1 & term_loan==1 // there are two observations per package-year: CL/term loan A and CL/terl loan B 

* table 8, column 5, at the firm level 

egen  maxtlBfad=sum(amt) if fpack==1 &  term_loan==1, by(borrower_id_mask year)
egen maxtlBfd=max(maxtlBfad) if fpack==1, by(borrower_id_mask year)

egen amtclf=sum(amt)  if cr_line==1 & fpack==1, by(borrower_id_mask year)
egen amt_clf=max(amtclf) if fpack==1, by(borrower_id_mask year)

g amt_diff_f=amt_clf/maxtlBfd

**************************************************************************
*** Credit Line Drawdowns (table 9) 
**************************************************************************

* CL utlization (table 9, columns 1-3)
g util=(amt_out)/amt_tot  if cr_line==1

sort loan_id_mask ctyr 
by loan_id_mask: g amt_outlag=amt_out[_n-1] if cr_line==1 & ctyr!=ctyr[_n-1] 
egen amtoutlagmax=max(amt_outlag), by(loan_id_mask year) //lagged

sort loan_id_mask ctyr 
by loan_id_mask: g amt_totlag=amt_tot[_n-1] if cr_line==1 & ctyr!=ctyr[_n-1]
egen amttotlagmax=max(amt_totlag), by(loan_id_mask year)  //lagged

g utillag=(amtoutlagmax)/amttotlagmax if cr_line==1  //lagged

g util_delta=util-utillag if cr_line==1 //table 9, change in the ratios 

* Utilization at the firm level (table 9 column 4) 

egen amt_outf=sum(amt_out)  if cr_line==1 & loan==1 & fpack==1, by(borrower_id_mask year)
egen amt_outfi=max(amt_outf), by(borrower_id_mask year)

g util_f=(amt_outfi)/amt_clf   //at the firm level 

* outstaning lagged   
egen amt_outlagf=sum(amtoutlagmax)  if cr_line==1 & loan==1 & fpack==1, by(borrower_id_mask year)
egen amt_outlagfi=max(amt_outlagf), by(borrower_id_mask year) //lagged outstanding at the firm level 
   
* total lagged 
sort borrower_id_mask boryr
by borrower_id_mask: gen lamt_clf= amt_clf[_n-1] if  boryr!=boryr[_n-1]  & fpack==1
egen lagamt_clflag=max(lamt_clf), by(borrower_id_mask year)
 
* drawdown firm measures  
g util_fl=(amt_outlagfi)/lagamt_clflag 
  
g util_deltaf=util_f- util_fl 


**************************************************************************
* Controls for Table B.4
**************************************************************************
* TL B Loan Amounts/All Deals: column 1

egen bk_termB1=sum(amt) if term_loanBplus1==1 & bank==1, by(year lender_id_mask)
	egen bk_termB=max(bk_termB1), by(year lender_id_mask)
egen bk_term1=sum(amt) if term_loan==1 & bank==1, by(year lender_id_mask)
	egen bk_term=max(bk_term1), by(year lender_id_mask)
g bk_ratio_termB=bk_termB/bk_term
	
* TL B Loan amounts/All Term Loans: column 2  

egen bk_termB_p_1=sum(amt) if packA_B==1 & bank==1, by(year lender_id_mask)
	egen bk_termB_pack=max(bk_termB_p_1), by(year lender_id_mask)
egen bk_term1AB=sum(amt) if packA_B!=. & bank==1, by(year lender_id_mask)
	egen bk_term_ABpack=max(bk_term1AB), by(year lender_id_mask)
g bk_ratio_termB_pack=bk_termB_pack/bk_term_ABpack


*********************************************************************
**** REGRESSIONS 
*********************************************************************

***********************************************************************
*** Table 3 Shadow Bank Shares and Term loan Packages (only term loans; loan-year level )
***********************************************************************
local vari secured_loan lnlsize matyr pass i.purp wbkroalead wbkncolead wlnassetslead wtier1_lead
* Column  1 
xi: reghdfe shadow_share  packA_B `vari'  if  loan==1 & term_loan==1, a(year)  vce(cluster borrower_id_mask)

* Column  2 
xi: reghdfe shadow_share packA_B  `vari' if loan==1 & term_loan==1, a(year borrower_id_mask lead_id_mask)  vce(cluster borrower_id_mask)

* Column  3
xi: reghdfe shadow_share packA_B `vari' if loan==1 & term_loan==1, a(boryr3 lead_id_mask)  vce(cluster borrower_id_mask)

* Column  4
xi: reghdfe shadow_share  term_loanBplus1   if loan==1 & term_loan==1 & packAB==1, a(packtime)  vce(cluster borrower_id_mask) 


***********************************************************************
*** Table 4 Riskiness of the Syndicate and Deal Tyoe: loan Spread (loan level; at issuance) 
***********************************************************************
local vari secured_loan lnlsize matyr pass i.purp wbkroalead wbkncolead wlnassetslead wtier1_lead

* Column  1 
xi: reghdfe aisd packA_B `vari'  if loan==1 & first_year==1 & term_loan==1, a(year)  vce(cluster borrower_id_mask)

* Column  2 
xi: reghdfe aisd packA_B `vari'  if loan==1 & first_year==1 & term_loan==1, a(year borrower_id_mask lead_id_mas)  vce(cluster borrower_id_mask)

* Column  3
xi: reghdfe aisd packA_B `vari'  if loan==1 & first_year==1 & term_loan==1, a(boryr3 lead_id_mas)  vce(cluster borrower_id_mask)

* Column 4 (within packet)
xi: reghdfe aisd i.term_loanBplus1   if loan==1 & first_year==1 & term_loan==1 & packAB==1, a(packtime)  vce(cluster borrower_id_mask)
local vari secured_loan lnlsize matyr pass i.purp wbkroalead wbkncolead wlnassetslead wtier1_lead

local vari secured_loan lnlsize matyr pass i.purp wbkroalead wbkncolead wlnassetslead wtier1_lead
* Column 5  
xi: reghdfe aisd packA_B `vari' if loan==1 & first_year==1 &  cr_line==1 , a(year)  vce(cluster borrower_id_mask)

* Column 6  
xi: reghdfe aisd packA_B  `vari'   if loan==1 & first_year==1 &  cr_line==1, a(year borrower_id_mask lead_id_mask)  vce(cluster borrower_id_mask)

* Column 7 
xi: reghdfe aisd packA_B  `vari'   if loan==1 & first_year==1 &  cr_line==1, a(boryr3 lead_id_mas lead_id_mas)  vce(cluster borrower_id_mask)

 
***********************************************************************
*** Table 5 Bank Capital and Deal Type (loan-year level) 
***********************************************************************
local vari_no_capital secured_loan lnlsize matyr pass i.purp wbkroalead wbkncolead wlnassetslead 

* Column  1 
xi: reghdfe capital  packA_B `vari_no_capital' if loan==1 & term_loan==1 & packA_B!=. , a(year)  vce(cluster borrower_id_mask)

* Column  2
xi: reghdfe capital  packA_B `vari_no_capital'  if  loan==1 & term_loan==1, a(year borrower_id_mask)  vce(cluster borrower_id_mask)
 
* Column  3
xi: reghdfe capital   packA_B `vari_no_capital'  if  loan==1 & term_loan==1, a(boryr3)  vce(cluster borrower_id_mask)

* Column  4
xi: reghdfe capital  term_loanBplus1     if  loan==1 & packAB==1 , a(packtime)  vce(cluster borrower_id_mask)

local vari_no_capital secured_loan lnlsize matyr pass i.purp wbkroalead wbkncolead wlnassetslead 
* Column  5
xi: reghdfe capital  packA_B  `vari_no_capital'  if loan==1 & cr_line==1 , a(year)  vce(cluster borrower_id_mask)

* Column  6
xi: reghdfe capital  packA_B `vari_no_capital'   if  loan==1 & cr_line==1, a(year borrower_id_mask lead_id_mas)  vce(cluster borrower_id_mask)

* Column  7
xi: reghdfe capital   packA_B `vari_no_capital'   if  loan==1 & cr_line==1, a(boryr3 lead_id_mas)  vce(cluster borrower_id_mask)


***************************************************************************
*** Table 6 Concentration of Bank Lenders and Deal Types (loan-year level) 
***************************************************************************
local vari secured_loan lnlsize matyr pass i.purp wbkroalead wbkncolead wlnassetslead wtier1_lead

* Column  1 
xi: reghdfe hhi  packA_B  `vari' if    loan==1 & term_loan==1     , a(year)  vce(cluster borrower_id_mask)

* Column  2
xi: reghdfe hhi    packA_B `vari' if   loan==1 & term_loan==1    , a(year borrower_id_mask lead_id_mas)

* Column  3
xi: reghdfe  hhi   packA_B `vari'  if   loan==1 & term_loan==1    , a(boryr3 lead_id_mas)  vce(cluster borrower_id_mask)

* Column  5
xi: reghdfe hhi   packA_B `vari' if   loan==1 & cr_line==1   , a(year)  vce(cluster borrower_id_mask)

local vari secured_loan lnlsize matyr pass i.purp wbkroalead wbkncolead wlnassetslead wtier1_lead
* Column  6
xi: reghdfe hhi i.packA_B `vari' if   loan==1 & cr_line==1   , a(year borrower_id_mask lead_id_mas)  vce(cluster borrower_id_mask)

* Column  7
xi: reghdfe hhi   packA_B `vari' if   loan==1 & cr_line==1  , a(boryr3 lead_id_mas)  vce(cluster borrower_id_mask)

* Column  4
xi: reghdfe hhi  term_loanBplus1  if  loan==1 & packAB==1 , a(packtime)  vce(cluster borrower_id_mask)

******************************************************************************************
*** Table 7 Credit Line Liquidity Provision: Credit Line and Term loan Maturity Overlap (deal-year level) 
*****************************************************************************************
local vari_deal wbkroaleadP wbkncoleadP lnassets_deal_lead wtier1_leadP pass_deal i.purpp
* Column  1 
xi: reghdfe mat_diff   packA_B  `vari_deal' if pack_count==1  , a(year) vce(cluster borrower_id_mask)

* Column  2
xi: reghdfe mat_diff    packA_B `vari_deal' if pack_count==1  , a(year borrower_id_mask lead_id_mas)  vce(cluster borrower_id_mask)

* Column  3
xi: reghdfe mat_diff     packA_B `vari_deal' if pack_count==1 , a(boryr3 lead_id_mas)  vce(cluster borrower_id_mask)


* Column  4 // package A&B 
xi: reghdfe mat_diffab term_loanBplus1   if  packAB==1  & loan==1 & term_loan==1, a(packtime) vce(cluster borrower_id_mask)

* Column  5  // at the firm level 
sort boryear
xi: reghdfe mat_diff_f tlb_firm   if boryear!=boryear[_n-1] & fpack==1, a(year borrower_id_mask ) vce(cluster borrower_id_mask)


******************************************************************************************
*** Table 8 Credit Line Liquidity Provision: Credit Line and Term loan Size (deal-year level) 
*****************************************************************************************
local vari_deal wbkroaleadP wbkncoleadP lnassets_deal_lead wtier1_leadP pass_deal i.purpp

* Column  1 
xi: reghdfe amt_diff   packA_B `vari_deal'  if pack_count==1  , a(year) vce(cluster borrower_id_mask)

* Column  2
xi: reghdfe amt_diff    packA_B `vari_deal' if pack_count==1  , a(year borrower_id_mask lead_id_mas)  vce(cluster borrower_id_mask)

* Column  3
xi: reghdfe amt_diff     packA_B `vari_deal' if pack_count==1 , a(boryr3 lead_id_mas)  vce(cluster borrower_id_mask)

* Column  4
xi: reghdfe amt_diffab term_loanBplus1   if  packAB==1  & loan==1 & term_loan==1, a(packtime) vce(cluster borrower_id_mask)

* Column  5  // at the firm level 
sort boryear
xi: reghdfe amt_diff_f tlb_firm   if boryear!=boryear[_n-1] & fpack==1, a(year borrower_id_mask ) vce(cluster borrower_id_mask)

******************************************************************************************
*** Table 9 Credit Line Liquidity Provision: Credit Line Drawdawns (only credit lines) (loan-year level) 
*****************************************************************************************
local vari secured_loan lnlsize matyr pass i.purp wbkroalead wbkncolead wlnassetslead wtier1_lead

* Column  1 
xi: reghdfe util_delta i.nber*packA_B `vari' if cr_line==1 & loan==1  , a(year) vce(cluster borrower_id_mask)

* Column  2
xi: reghdfe  util_delta  i.nber*packA_B `vari'  if loan==1 & cr_line==1   , a(year borrower_id_mask lead_id_mas)  vce(cluster borrower_id_mask)

* Column  3
xi: reghdfe util_delta    i.nber*packA_B `vari'  if loan==1 & cr_line==1, a(boryr3 lead_id_mas)  vce(cluster borrower_id_mask)

* Column  4 // at the firm level 
sort boryear
xi: reghdfe util_deltaf i.nber*tlb_firm   if boryear!=boryear[_n-1] & fpack==1, a(year borrower_id_mask )  vce(cluster borrower_id_mask)


*********************************************************************************************
*** FIGURES 
**********************************************************************************************
*** Figure 1 Total loan Amounts 
*****************************************************************************************

* billion 
egen amt_btot2=sum(amt/1000000)  if term_loanBplus1==1, by(year)
egen amt_totB2=max(amt_btot2), by(year)

egen amt_A=sum(amt/1000000)  if term_loanA==1 , by(year)
egen amt_A1=max(amt_A), by(year)

egen clamt_btotV=sum(amt/1000000)  if cr_line==1, by(year)
egen clamt_totV=max(clamt_btotV), by(year)

sort year
twoway line   amt_totB2 amt_A1  clamt_totV  year if year!=year[_n-1] & year>=1991,  title("Total loan Amounts") note("") graphregion(color(white)) bgcolor(white) ytitle("billion") xtitle("") legend(pos(6) col(3) label(1 "Term-B loans") label(2 "Term-A loans ") label(3 "Credit Lines") )  xlabel(1991 (4) 2016)


**********************************************************************************************
*** Figure 2 All-in-Drawn Spreads
*****************************************************************************************
* at the term loans level 
egen spr_tlb1=mean(aisd) if term_loanBplus1==1 & first_year==1 & loan==1 , by(year)
egen spr_tlb=max(spr_tlb1),  by(year)

egen spr_tla1=mean(aisd) if  term_loanA==1 & first_year==1 & loan==1, by(year)
egen spr_tla=max(spr_tla1),  by(year)

egen spr_cl=mean(aisd) if  cr_line==1 & first_year==1 & loan==1 , by(year)
egen spr_cr_line=max(spr_cl),  by(year)

sort year
twoway line  spr_tlb spr_tla spr_cr_line year if year!=year[_n-1] & year>=1991,  title("All-in-Drawn Spreads") graphregion(color(white)) bgcolor(white) ytitle("basis points") xtitle("") legend(pos(6) col(3) label(1 "Term-B loans") label(2 "Term-A loans")  label(3 "Credit Line"))  xlabel(1991 (4) 2016)
  

**********************************************************************************************
*** Figure 3 Average Shadow Bank Shares 
*****************************************************************************************

egen sh_b1=mean(shadow_share*100) if term_loanBplus1==1 & loan==1 , by(year)
egen sh_share_b=max(sh_b1),  by(year)

egen sh_a1=mean(shadow_share*100) if  term_loanA==1 &  loan==1, by(year)
egen sh_share_a=max(sh_a1),  by(year)


sort year
twoway line  sh_share_b sh_share_a  year if year!=year[_n-1] & year>=1991,  title("Average Shadow Bank Shares ") graphregion(color(white)) bgcolor(white) ytitle("percent") xtitle("") legend(pos(6) col(3) label(1 "Term-B loans") label(2 "Term-A loans") )  xlabel(1991 (4) 2017)

**********************************************************************************************
*** Figure 4 Within-deal difference between credit lines and term loans 
*****************************************************************************************

 * HISTOGRAM  
twoway (hist mat_diff if pack_count==1 &  packA_B==1  & mat_diff>-3 & mat_diff<3,   percent bin(20) lcolor(gs12) fcolor(blue))(hist mat_diff if pack_count==1 & packA_B==0  & mat_diff>-3 & mat_diff<3  , percent bin(20) lcolor(red) fcolor(none)), title("Maturity Difference: Credit Lines-Term loans") xtitle("maturity difference in years")
* trim at 1% for the graph 
**********************************************************************************************
*** Figure 6 Within-deal Ratio of Credit Line and Term loans Committed Amounts. 
*****************************************************************************************

* HISTOGRAM  
twoway (hist amt_diff if pack_count==1 &  packA_B==1 & amt_diff<4,   percent bin(20) lcolor(gs12) fcolor(gs12))(hist amt_diff if pack_count==1 & packA_B==0 & amt_diff<4 , percent bin(20) lcolor(red)fcolor(none)), title("Amount Difference: Credit Line Amount/Term loan Amount") xtitle("fraction")


*********************************************************************************************
*** Appendix B 
**********************************************************************************************

******************************************************************************************
*** Table B.3 Bank Capital (Alternative Definition) and Deal Type  (loan-year level) 
*****************************************************************************************
local vari_no_capital secured_loan lnlsize matyr pass i.purp wbkroalead wbkncolead wlnassetslead 
*Column  1
xi: reghdfe  capital_alt  packA_B `vari_no_capital' if loan==1 & term_loan==1 , a(year)  vce(cluster borrower_id_mask)

*Column  2
xi: reghdfe  capital_alt packA_B `vari_no_capital'  if  loan==1 & term_loan==1, a(year borrower_id_mask)  vce(cluster borrower_id_mask)
 
*Column  3
xi: reghdfe  capital_alt   packA_B `vari_no_capital'  if  loan==1 & term_loan==1 , a(boryr3)  vce(cluster borrower_id_mask)

*Column  5
xi: reghdfe capital_alt  packA_B `vari_no_capital'   if  loan==1 & cr_line==1 , a(year)  vce(cluster borrower_id_mask)

*Column  6
xi: reghdfe capital_alt  packA_B `vari_no_capital' if  loan==1 & cr_line==1, a(year borrower_id_mask lead_id_mas)  vce(cluster borrower_id_mask)

*Column  7
xi: reghdfe capital_alt   packA_B `vari_no_capital'  if  loan==1 & cr_line==1, a(boryr3 lead_id_mas)  vce(cluster borrower_id_mask)
 
*Column  4
xi: reghdfe   capital_alt  term_loanBplus1     if  loan==1 & packAB==1 , a(packtime)  vce(cluster borrower_id_mask)


******************************************************************************************
*** Table B.4 Bank Level Regressions: Bank Capital and Term-B loan portfolio   (bank-year level) 
*****************************************************************************************
* Column 1
xi: reghdfe tier1_q4_bank bk_ratio_termB lnassets bknco bkroa if  bkyr_1==1, a(year)  vce(cluster lender_id_mask)

*Column 2
xi: reghdfe tier1_q4_bank bk_ratio_termB_pack lnassets bknco bkroa if  bkyr_1==1, a(year)  vce(cluster lender_id_mask)

******************************************************************************************
*** Table B.5 Number of Bank Lenders and Deal  (loan-year level) 
*****************************************************************************************
local vari secured_loan lnlsize matyr pass i.purp wbkroalead wbkncolead wlnassetslead wtier1_lead

* Column  1 
xi: reghdfe ln_numb_bks  packA_B  `vari' if    loan==1 & term_loan==1     , a(year)  vce(cluster borrower_id_mask)

* Column  2
xi: reghdfe ln_numb_bks    packA_B `vari' if   loan==1 & term_loan==1    , a(year borrower_id_mask lead_id_mas)

* Column  3
xi: reghdfe ln_numb_bks   packA_B `vari'  if   loan==1 & term_loan==1    , a(boryr3 lead_id_mas)  vce(cluster borrower_id_mask)
 
* Column  5
xi: reghdfe ln_numb_bks  packA_B `vari' if   loan==1 & cr_line==1   , a(year)  vce(cluster borrower_id_mask)

* Column  6
xi: reghdfe ln_numb_bks i.packA_B `vari' if   loan==1 & cr_line==1   , a(year borrower_id_mask lead_id_mas)  vce(cluster borrower_id_mask)

* Column  7
xi: reghdfe ln_numb_bks  `vari' if   loan==1 & cr_line==1  , a(boryr3 lead_id_mas)  vce(cluster borrower_id_mask)

* Column  4
xi: reghdfe ln_numb_bks  term_loanBplus1  if  loan==1 & packAB==1 , a(packtime)  vce(cluster borrower_id_mask)







